﻿using BigEgg.Framework.Applications;
using BigEgg.Framework.Applications.ViewModels;
using FMStudio.Templates.DesignTool.Application.Properties;
using FMStudio.Templates.DesignTool.Application.Services;
using FMStudio.Templates.DesignTool.Application.Views;
using System;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.Windows.Input;

namespace FMStudio.Templates.DesignTool.Application.ViewModels
{
    [Export]
    public class ShellViewModel : ViewModel<IShellView>
    {
        #region Fields
        private readonly IShellService shellService;
        private readonly IFileService fileService;

        private object projectExplorerView;
        private object toolBoxView;
        private object propertyView;

        private ICommand closeCommand;
        private ICommand aboutCommand;
        #endregion

        [ImportingConstructor]
        public ShellViewModel(IShellView view, IFileService fileService, IPresentationService presentationService, IShellService shellService)
            : base(view)
        {
            this.shellService = shellService;
            this.fileService = fileService;
            view.Closing += ViewClosing;
            view.Closed += ViewClosed;

            //  Restore the window size when the values are valid.
            if (Settings.Default.Left >= 0 && Settings.Default.Top >= 0 && Settings.Default.Width >= 0 && Settings.Default.Height >= 0
                && Settings.Default.Left + Settings.Default.Width <= presentationService.VirtualScreenWidth
                && Settings.Default.Top + Settings.Default.Height <= presentationService.VirturalScreenHeight)
            {
                this.ViewCore.Left = Settings.Default.Left;
                this.ViewCore.Top = Settings.Default.Top;
                this.ViewCore.Width = Settings.Default.Width;
                this.ViewCore.Height = Settings.Default.Height;
            }
            this.ViewCore.IsMaximized = Settings.Default.IsMaximized;
        }

        #region Properties
        public string ApplicationTitle { get { return ApplicationInfo.ProductName; } }

        public IShellService ShellService { get { return this.shellService; } }

        public object ProjectExplorerView
        {
            get { return this.projectExplorerView; }
            set
            {
                if (this.projectExplorerView != value)
                {
                    this.projectExplorerView = value;
                    RaisePropertyChanged("ProjectExplorerView");
                }
            }
        }

        public object ToolBoxView
        {
            get { return this.toolBoxView; }
            set
            {
                if (this.toolBoxView != value)
                {
                    this.toolBoxView = value;
                    RaisePropertyChanged("ToolBoxView");
                }
            }
        }

        public object PropertyView
        {
            get { return this.propertyView; }
            set
            {
                if (this.propertyView != value)
                {
                    this.propertyView = value;
                    RaisePropertyChanged("PropertyView");
                }
            }
        }

        public ICommand CloseCommand
        {
            get { return this.closeCommand; }
            set
            {
                if (this.closeCommand != value)
                {
                    this.closeCommand = value;
                    RaisePropertyChanged("CloseCommand");
                }
            }
        }

        public ICommand AboutCommand
        {
            get { return this.aboutCommand; }
            set
            {
                if (this.aboutCommand != value)
                {
                    this.aboutCommand = value;
                    RaisePropertyChanged("AboutCommand");
                }
            }
        }
        #endregion

        #region Events
        public event CancelEventHandler Closing;
        #endregion

        #region Methods
        #region Public Methods
        public void Show()
        {
            this.ViewCore.Show();
        }

        public void Close()
        {
            this.ViewCore.Close();
        }
        #endregion

        #region Protect Methods
        protected virtual void OnClosing(CancelEventArgs e)
        {
            if (this.Closing != null) { this.Closing(this, e); }
        }
        #endregion

        #region Private Methods
        private void ViewClosing(object sender, CancelEventArgs e)
        {
            OnClosing(e);
        }

        private void ViewClosed(object sender, EventArgs e)
        {
            Settings.Default.Left = this.ViewCore.Left;
            Settings.Default.Top = this.ViewCore.Top;
            Settings.Default.Width = this.ViewCore.Width;
            Settings.Default.Height = this.ViewCore.Height;
            Settings.Default.IsMaximized = this.ViewCore.IsMaximized;
        }
        #endregion
        #endregion
    }
}
